package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.context.BaseContext;
import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.CategoryMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.CategoryService;
import com.sky.vo.CategoryListVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class CategoryServiceImpl implements CategoryService {
    @Autowired
    private CategoryMapper categoryMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;


    @Override
    public PageResult<Category> page(CategoryPageQueryDTO categoryPageQueryDTO) {
        PageHelper.startPage(categoryPageQueryDTO.getPage(), categoryPageQueryDTO.getPageSize());

        String name = categoryPageQueryDTO.getName();
        Integer type = categoryPageQueryDTO.getType();

        Page<Category> categoryPage = categoryMapper.page(name, type);
        PageResult<Category> pageResult = new PageResult<>();
        pageResult.setTotal(categoryPage.getTotal());
        pageResult.setRecords(categoryPage.getResult());
        return pageResult;

    }

    @Override
    public void add(CategoryDTO categoryDTO) {
        Category category = new Category();
        BeanUtils.copyProperties(categoryDTO, category);
        category.setCreateTime(LocalDateTime.now());
        category.setUpdateTime(LocalDateTime.now());
        category.setCreateTime(LocalDateTime.now());

        Long currentId = BaseContext.getCurrentId();
        category.setCreateUser(currentId);
        category.setUpdateUser(currentId);

        categoryMapper.insert(category);

    }

    @Override
    public void status(Integer status, Long id) {
        Category category = new Category();
        category.setStatus(status);
        category.setId(id);
        category.setUpdateTime(LocalDateTime.now());

        Long currentId = BaseContext.getCurrentId();
        category.setUpdateUser(currentId);

        categoryMapper.update(category);
    }

    @Override
    public void delete(Long id) {
        //查看菜品表中是否有该分类，有的话，则不能删除
      List<Dish> dishList = dishMapper.selectByCategoryId(id);
        if(dishList.size() > 0){
            throw new DeletionNotAllowedException("该分类下有菜品，不能删除");
        }
        //查看套餐表中是否有该分类，有的话，则不能删除
        List<Setmeal> setmealList = setmealMapper.selectByCategoryId(id);
        if(setmealList.size() > 0){
            throw new DeletionNotAllowedException("该分类下有套餐，不能删除");
        }

        categoryMapper.delete(id);


    }

    @Override
    public void update(CategoryDTO categoryDTO) {
        Category category = new Category();
        BeanUtils.copyProperties(categoryDTO, category);
        category.setUpdateTime(LocalDateTime.now());

        Long currentId = BaseContext.getCurrentId();
        category.setUpdateUser(currentId);

        categoryMapper.update(category);
    }

    @Override
    public List<Category> list(Integer type) {
        return categoryMapper.list(type);
    }

    @Override
    public List<Category> categoryList(Integer type) {
       return categoryMapper.list(type);
    }
}
